/**
 * 从1到N，
 * 首先拿掉1,3,5,7,...
 * 然后2,6,10,14,...
 * 然后4,12,20,...
 * 然后8,16,...
 * ...
 * 问第K个拿掉数是几
 * 分别是2、4、8、16、...的等差数列，计算一下即可
 * 每个kase需要log时间
 */
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;

namespace IO{

char *__abc147, *__xyz258, __ma369[1000000];
#define __hv007() ((__abc147==__xyz258) && (__xyz258=(__abc147=__ma369)+fread(__ma369,1,100000,stdin),__abc147==__xyz258) ? EOF : *__abc147++)

int getUnsigned(){
	int sgn = 1;
	char ch = __hv007();
	while(ch < '0' || ch > '9') ch = __hv007();
	 
	int ret = (int)(ch-'0');
	while( '0' <= (ch=__hv007()) && ch <= '9' ) ret = ret * 10 + (int)(ch-'0');
	return ret;
}


char __pq981[1000000];
char const * const __rs721 = __pq981 + 1000000;
char * __qw752 = __pq981;

#define __ge(c) (__qw752 == __rs721 ? fwrite(__pq981, 1, 1000000, stdout), __qw752 = __pq981 + 1, *__pq981 = (c) : *__qw752++ = (c))

void putUnsignedEndl(int x){
    if(x == 0) return (void)(__ge('0'));

    char st[25];
	int top = 0;
	while(x){
		st[top++] = x % 10 + '0';
		x /= 10;
	} 
	while(top) __ge(st[--top]);
    __ge('\n');
	return;
}

void finish(){
	fwrite(__pq981, 1, __qw752 - __pq981, stdout);
}

}

int N;
int K;

int proc(){
    int b = 0;
    while(1){
        int c = N / 2;
        if(N & 1) c += 1;
        if(K <= c) return K + K - 1 << b;

        b += 1;
        N >>= 1;
        K -= c;
    }
    return -1;
}

void work(){
    N = IO::getUnsigned();
    K = IO::getUnsigned();
    IO::putUnsignedEndl(proc());
    return;
}



int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    // ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int nofkase = IO::getUnsigned();
    while(nofkase--) work();
    IO::finish();
    return 0;
}